package com.klun.concTrueWar.six;

import jdk.nashorn.internal.ir.RuntimeNode;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;

/**
 * 支持关闭操作的 Web 服务器
 * 使用了 ExecutorService 可以查看执行状态
 */
public class LifecycleWebServer {
    private final ExecutorService exec = new ExecutorService() {
        @Override
        public void shutdown() {

        }

        @Override
        public List<Runnable> shutdownNow() {
            return null;
        }

        @Override
        public boolean isShutdown() {
            return false;
        }

        @Override
        public boolean isTerminated() {
            return false;
        }

        @Override
        public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
            return false;
        }

        @Override
        public <T> Future<T> submit(Callable<T> task) {
            return null;
        }

        @Override
        public <T> Future<T> submit(Runnable task, T result) {
            return null;
        }

        @Override
        public Future<?> submit(Runnable task) {
            return null;
        }

        @Override
        public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
            return null;
        }

        @Override
        public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException {
            return null;
        }

        @Override
        public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
            return null;
        }

        @Override
        public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
            return null;
        }

        @Override
        public void execute(Runnable command) {

        }
    };

    public void start() throws IOException{
        ServerSocket socket = new ServerSocket(8848);
        while (!exec.isShutdown()){
            try{
                final Socket conn = socket.accept();
                exec.execute(new Runnable() {
                    @Override
                    public void run() {
                        handleRequest(conn);
                    }
                });
            } catch (RejectedExecutionException e){
                if(!exec.isShutdown()){
                    System.out.println("task submission rejected"+e);
                }
            }
        }
    }
    public void stop(){
        exec.shutdown();
    }
    public void handleRequest(Socket connection){
        RuntimeNode.Request req = readRequest(connection);
        if(isShutdownRequest(req)){
            stop();
        }else
            dispatchRequest(req);
        System.out.println("shoudaoqingqiu");

    }

    private RuntimeNode.Request readRequest(Socket connection) {
        return null;
    }

    private boolean isShutdownRequest(RuntimeNode.Request req) {
        return false;
    }

    private void dispatchRequest(RuntimeNode.Request req) {
    }

}
